package card_discount

import (
	// "bp/cache"
	"bp/conf"
	"bp/db"

	// "bgs/grpc/client"
	internalHttpMiddleware "bp/internal/middleware/http"
	"bp/internal/service/com"
	"bp/internal/service/entity"
	"bp/internal/twirpex"
	"go.uber.org/zap"

	// "bp/internal/util"
	cardDiscountGrpc "bp/rpc/gen/mwebapi/mcard_discount"
	messageGrpc "bp/rpc/gen/shared/message"
	"context"

	"github.com/go-chi/chi"
	"github.com/golang/protobuf/ptypes/empty"
)

type CardDiscountMWebApiImp struct {
	dao       *db.PgDao
	slog      *zap.SugaredLogger
	vars      *conf.VarsSection
	entityHub *entity.EntityHub
	comHub    *com.ComHub
	// cacher *cache.Adapter
	cardDiscountGrpc.CardDiscountOpService
}

// NewCardDiscountMWebApi initialization
func NewCardDiscountMWebApi(
	slog *zap.SugaredLogger,
	pgdao *db.PgDao,
	vars *conf.VarsSection,
	entityHub *entity.EntityHub,
	comHub *com.ComHub,
	// adp *cache.Adapter,
) (ws *CardDiscountMWebApiImp) {
	ws = &CardDiscountMWebApiImp{
		dao:       pgdao,
		slog:      slog.With("module", "CardDiscountMWebApi"),
		vars:      vars,
		entityHub: entityHub,
		comHub:    comHub,
		// cacher:           adp,
	}
	return
}

const (
	EntityName                       string = "卡折扣"
	PingMethod                       string = "/card_discount/ping/exec"          // GET
	GetCardDiscountByOutCardIDMethod string = "/card_discount/{out_card_id}" // GET
)

// RegisterToWeb 将部分grpc方法注册到web
func (s *CardDiscountMWebApiImp) RegisterToWeb(r chi.Router) {
	cardDiscountRT := twirpex.NewSingleTwirpServerProxy(cardDiscountGrpc.NewCardDiscountOpServiceServer(s))

	r.Group(func(r chi.Router) {
		r.Get(PingMethod, cardDiscountRT.Handle("PingCardDiscount"))
		r.Group(func(r chi.Router) {
			r.Use(internalHttpMiddleware.WithAuthorizationToken(s.slog, s.vars))
			r.Get(GetCardDiscountByOutCardIDMethod, cardDiscountRT.Handle("GetCardDiscountByOutCardID"))
		})
	})
}

func (s *CardDiscountMWebApiImp) PingCardDiscount(ctx context.Context, _ *empty.Empty) (res *messageGrpc.PingRes, err error) {
	res = &messageGrpc.PingRes{
		Msg: "ok",
	}
	return
}
